Completed
Push — master ( 56c982...731a12 )
by Matthew
03:00
created

autoedits.js ➔ $   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 51
rs 9.4109
cc 2
nc 2
nop 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
B autoedits.js ➔ ... ➔ $(ꞌ.tools-toggleꞌ).click 0 41 3
A autoedits.js ➔ ... ➔ $.then 0 3 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
$(function() {
2
    var newData;
3
4
    $('.tools-table').on('click', '.tools-toggle', function () {
5
        if (!newData) {
6
            // countsByTool must be cloned
7
            newData = Object.assign({}, window.countsByTool);
8
        }
9
10
        var index = $(this).data('index'),
11
            tool = $(this).data('tool'),
12
            $row = $(this).parents('tr');
0 ignored issues
show
Unused Code introduced by
The variable $row seems to be never used. Consider removing it.
Loading history...
13
14
        // must use .attr instead of .prop as sorting script will clone DOM elements
15
        if ($(this).attr('data-disabled') === 'true') {
16
            newData[tool] = window.countsByTool[tool];
17
            window.toolsChart.data.datasets[0].data[index] = parseInt(newData[tool], 10);
18
            $(this).attr('data-disabled', 'false');
19
        } else {
20
            delete newData[tool];
21
            window.toolsChart.data.datasets[0].data[index] = null;
22
            $(this).attr('data-disabled', 'true');
23
        }
24
25
        // gray out row in table
26
        $(this).parents('tr').toggleClass('excluded');
27
28
        // change the hover icon from a 'x' to a '+'
29
        $(this).find('.glyphicon').toggleClass('glyphicon-remove').toggleClass('glyphicon-plus');
30
31
        // update stats
32
        var total = 0;
33
        Object.keys(newData).forEach(function (tool) {
34
            total += parseInt(newData[tool], 10);
35
        });
36
        var toolsCount = Object.keys(newData).length;
37
        $('.tools--tools').text(
38
            toolsCount.toLocaleString() + " " +
39
            $.i18n('num-tools', toolsCount)
40
        );
41
        $('.tools--count').text(total.toLocaleString());
42
43
        window.toolsChart.update();
44
    });
45
46
    if ($('.non-auto-edits-container')[0]) {
47
        $.getJSON('/api/nonautomated_edits/en.wikipedia.org/L235/all/0').then(function (data) {
48
            $('.non-auto-edits-container').html(data.markup);
49
        });
50
    }
51
});
52